<template>
  <div class="container">
    <div class="app-container">
      <!-- 树形结构 -->
      <el-tree :expand-on-click-node="false" default-expand-all :data="detps" :props="defaultProps">
        <template #default="{data}">
          <el-row type="flex" style="width: 100%; height: 40px;" align="middle" justify="space-between">
            <span>{{ data.name }}</span>

            <!-- 负责人 -->
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>

              <!-- 下拉菜单 -->
              <el-dropdown @command="handleCommand(data.id, $event)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <addDept ref="addDept" :current-node-id="currentNodeId" :show-dialog.sync="showDialog" @updateDepartment="getDepartments" />
  </div>
</template>
<script>
import { delDepartment, getDepartments } from '@/api/department'
import { translateListToTreeData } from '@/utils'
import addDept from './components/add-dept.vue'

export default {
  name: 'Department',
  components: {
    addDept
  },
  data() {
    return {
      detps: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      currentNodeId: null // 存储当前点击的id
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取部门列表
    async getDepartments() {
      const result = await getDepartments()
      console.log(result)
      this.detps = translateListToTreeData(result, 0)
    },
    // 下拉菜单 -操作
    handleCommand(id, type) {
      console.log(id, type)
      if (type === 'add') {
        // 添加子部门
        this.showDialog = true // 显示弹框
        this.currentNodeId = id
      } else if (type === 'edit') {
        // 编辑部门
        this.showDialog = true
        this.currentNodeId = id
        // 调用子组件身上的获取部门详情的方法（表单渲染）
        // currentNodeId null
        /*  原因: 显示子组件，内部的数据/方法，并没有构建完成
               父组件更新子组件的props ==> 异步的
         解决方案:
         让子组件完全构建成功，再调用子组件身上的方法
         晚一点调用子组件的方法.…...等它完全渲染完成 */
        this.$nextTick(() => {
          // vue更新dom的过程是异步的
          // 批量的更新 - 提高更新性能
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        // 删除部门
        this.$confirm('您确定要删除该部门嘛？').then(async() => {
          await delDepartment(id)
          // 提示信息
          this.$message.success('删除部门成功')
          this.getDepartments()
        })
      }
    }
  }
}
</script>
<style scoped lang="scss">
 .app-container{
  padding: 30px 135px;
  font-size: 14px;
 }
 .tree-manager{
  width: 80px;
  display: inline-block;
  margin: 10px;
 }
</style>
